package com.example.growthmuseum.museum;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.example.growthmuseum.AppController;
import com.example.growthmuseum.R;
import com.example.growthmuseum.ServiceFactory;
import com.example.growthmuseum.data.model.Goods;
import com.example.growthmuseum.data.model.Result;
import com.example.growthmuseum.data.model.User;
import com.example.growthmuseum.goodsdesc.GoodsDescActivity;
import com.example.growthmuseum.service.GoodsService;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.orhanobut.logger.Logger;

import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Jacob on 2016/10/29.
 */

public class MuseumFragment extends Fragment
        implements SwipeRefreshLayout.OnRefreshListener{

    private MuseumAdapter museumAdapter;

    private EasyRecyclerView recyclerView;

    private GoodsService goodsService;

    public MuseumFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static MuseumFragment newInstance() {
        return new MuseumFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_museum, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        museumAdapter = new MuseumAdapter(getActivity());
        this.goodsService = ServiceFactory
                .createRetrofitService(GoodsService.class,
                        ServiceFactory.BASE_URL);
        initViews(view);
        onRefresh();
    }

    private void initViews(View view) {
        // Set up the competitions view
        recyclerView = (EasyRecyclerView) view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapterWithProgress(museumAdapter);
        recyclerView.setRefreshListener(this);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        museumAdapter.setNoMore(R.layout.view_no_more);
        museumAdapter.setOnItemClickListener(
                this::toGoodsActivity);

    }

    private void toGoodsActivity(int position) {
        Intent toGoodsDesc = new Intent(getActivity(), GoodsDescActivity.class);
        toGoodsDesc.putExtra("id", museumAdapter.getItem(position).getId());
        startActivity(toGoodsDesc);
    }

    @Override
    public void onRefresh() {
        goodsService.getGoodsList()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Result<List<Goods>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Logger.e(e, e.getMessage());

                    }

                    @Override
                    public void onNext(Result<List<Goods>> result) {
                        if (result.getCode().equals(Result.SUCCESS)) {
                            setGoodsList(result.getBean());
                        } else {
                            Toast.makeText(getActivity(),
                                    result.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    private void setGoodsList(List<Goods> goodsList) {
        museumAdapter.clear();
        museumAdapter.addAll(goodsList);
        museumAdapter.notifyDataSetChanged();
    }
}
